package com.tjp.eight.net;

import org.json.JSONException;

import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/2/13.
 */

public class SubscribeCallBack<T> extends Subscriber<T> {
    public NetCallBack<T> netCallBack;

    public SubscribeCallBack(NetCallBack<T> netCallBack) {
        this.netCallBack = netCallBack;
    }

    @Override
    public void onCompleted() {
        netCallBack.onCompleted();
    }

    @Override
    public void onError(Throwable e) {
        int code = 404;
        String msg = "未知错误";
        if (e instanceof HttpException ||e instanceof UnknownHostException || e instanceof UnknownServiceException) {
            code = 500;
            msg = "哎呀！网络\n被洪荒之兽吞噬了~";
        }else if (e instanceof JSONException){
            msg = "服务器返回\n数据错误！";
        }
        netCallBack.onFails(code, msg);
        netCallBack.onCompleted();
    }

    @Override
    public void onNext(T t) {
        netCallBack.onSuccess(t);
    }
}
